package com.sfh.cp.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter.OnItemClickListener;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sfh.cp.R;

import crm.sfh.server.entity.ENote;

import com.sfh.cp.util.UtilFace;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 发布内容-图片形式
 */

public class WidgetContentImageLayout extends LinearLayout implements View.OnClickListener,OnItemClickListener {


    ImageAdapter imageAdapter;

    TextView tvContent;

    TextView tvLabel;

    TextView tvReading;

    TextView tvFabulous;

    TextView tvMessage;

    int width;

    private RecyclerView recycler;
    IContentOnClickLayout onClickLayout;

    ENote contentType;

    public WidgetContentImageLayout(Context context) {

        super(context);
        init();
    }

    public WidgetContentImageLayout(Context context, @Nullable AttributeSet attrs) {

        super(context, attrs);
        init();
    }

    public WidgetContentImageLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {

        inflate(this.getContext(), R.layout.widget_content_image_layout, this);
        this. recycler =   this.find(R.id.recycler);

        this.tvContent = this.find(R.id.tvContent);
        this.tvLabel = this.find(R.id.tvLabel);

        this.tvReading = this.find(R.id.tvReading);
        this.tvFabulous = this.find(R.id.tvFabulous);
        this.tvMessage = this.find(R.id.tvMessage);

        this.tvFabulous.setOnClickListener(this);
        this.tvMessage.setOnClickListener(this);

        this.width = getResources().getDisplayMetrics().widthPixels;
        this.setTitltmaxLines(5);
        this.imageAdapter = new ImageAdapter();
        this.recycler.setAdapter(imageAdapter);
        this.recycler.addItemDecoration(new SpaceItemDecoration(this.getResources().getDimensionPixelSize(R.dimen.x6)));
        this.imageAdapter.setOnItemClickListener(this);
    }

    private <T> T find(int r) {

        return (T) findViewById(r);
    }


    public void showData(ENote contentType) {

        this.contentType = contentType;
        if (contentType == null || TextUtils.isEmpty(contentType.getContent())) {
            this.setVisibility(GONE);
            return;
        }

        List<String> images = Arrays.asList(contentType.getContent().split(";"));

        this.setVisibility(VISIBLE);
        if (TextUtils.isEmpty(contentType.getTitle())) {
            this.tvContent.setVisibility(GONE);
        } else {
            this.tvContent.setVisibility(VISIBLE);
            SpannableString spannableString = UtilFace.getEmotionContent(getContext(), this.tvContent, contentType.getTitle());
            this.tvContent.setText(spannableString);
        }

        this.tvLabel.setVisibility(TextUtils.isEmpty(contentType.getLable()) ? GONE : VISIBLE);
        this.tvLabel.setText(String.format("〇 %s", contentType.getLable()));

        this.tvReading.setText(String.format("浏览%s次", contentType.getSizeReading()));
        this.tvFabulous.setText(contentType.getSizeFabulous() + "");
        this.tvMessage.setText(contentType.getSizeMessage() + "");

        this.tvFabulous.setCompoundDrawablesWithIntrinsicBounds(this.contentType.isFabulous()?R.mipmap.like_select:R.mipmap.like,0,0,0);

        final int size = images.size();
        // 1. 默认图片大小- 边距
        int ivWidth = width - this.getResources().getDimensionPixelSize(R.dimen.x20);
        if (size > 1) {
            // 图片大小= 屏幕宽度3/1 - 间距固定值
            ivWidth = ivWidth / 3 - this.getResources().getDimensionPixelSize(R.dimen.x6);
            this.recycler.setLayoutManager( new GridLayoutManager(getContext(), 3));
        }else{
            this.recycler.setLayoutManager( new LinearLayoutManager(getContext()));
        }

        imageAdapter.setIvWidth(ivWidth);
        imageAdapter.replaceData(images);

    }


    public void setOnClickLayout(IContentOnClickLayout onClickLayout) {

        this.onClickLayout = onClickLayout;
    }

    public void setTitltmaxLines(int maxLines) {

        this.tvContent.setMaxLines(maxLines);
        this.tvContent.setEllipsize(TextUtils.TruncateAt.END);
    }

    @Override
    public void onClick(View v) {

        if (this.onClickLayout == null || contentType == null) {
            return;
        }

        if (v == this.tvFabulous) {
            this.tvFabulous.setText(String.valueOf(contentType.getSizeFabulous() +1));
            this.contentType.setUserFabulous(true);
            this.tvFabulous.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.like_select,0,0,0);
            this.onClickLayout.onClickFabulous(v, contentType);
            return;
        }

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (this.onClickLayout == null) {
            return;
        }
        this.onClickLayout.onClickImage(view,contentType,position);
    }
    class ImageAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

        int ivWidth;

        public void setIvWidth(int ivWidth) {
            this.ivWidth = ivWidth;
        }

        public ImageAdapter() {
            super(R.layout.widget_image_item);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {

            ImageView iv = helper.getView(R.id.ivItem);
            int ivHeight = getData().size() == 1 ? ivWidth / 3 * 2 : ivWidth;
            FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) iv.getLayoutParams();
            layoutParams.height = ivHeight;
            layoutParams.width = ivWidth;
            iv.setLayoutParams(layoutParams);
            Picasso.with(getContext())
                    .load(item).config(Bitmap.Config.RGB_565)
                    .into(iv);
        }
    }
    public class SpaceItemDecoration extends RecyclerView.ItemDecoration {

        private int space;

        public SpaceItemDecoration(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            //不是第一个的格子都设一个左边和底部的间距
            outRect.left = space;
            outRect.bottom = space;
            //由于每行都只有3个，所以第一个都是3的倍数，把左边距设为0
            if (parent.getChildLayoutPosition(view) %3==0) {
                outRect.left = 0;
            }
        }

    }

}
